package com.example.yiqiuser.service.impl;

import com.example.yiqiuser.dao.ScenicDao;
import com.example.yiqiuser.entity.Pic;
import com.example.yiqiuser.entity.Scenic;
import com.example.yiqiuser.entity.TScenicTicket;
import com.example.yiqiuser.service.ScenicService;
import com.example.yiqiuser.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: yiqiuser
 * @description:
 * @author: 诸葛龙辰
 * @create: 2023-03-30 17:10
 * @version:
 **/
@Service
public class ScenicServiceImpl implements ScenicService {


    @Resource
    private ScenicDao scenicDao;


    /**
     * 根据票数降序排列带参查询
     * @param page
     * @return
     */
    public PageInfo recommendShow(Page<Scenic> page){
        //设置页码和每页显示数量
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(scenicDao.recommendShow(page.getData()));
    }



    /**
     * 根据价格升序带参查询
     * @param page
     * @return
     */
    public PageInfo priceShow(Page<Scenic> page){
        //设置页码和每页显示数量
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(scenicDao.priceShow(page.getData()));
    }


    /**
     *
     * @param page
     * @return
     */
    @Override
    public PageInfo queryPage(Page<Scenic> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(scenicDao.queryByParam(page.getData()));
    }
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Scenic> queryAll() {
        return scenicDao.queryAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Scenic queryById(Integer id) {
        List<Scenic> scenicList  = scenicDao.queryById(id);
         //判断集合是否为null
        if (scenicList!=null&&scenicList.size()>0){
            //返回查询到的一条数据
          Scenic scenic = scenicList.get(0);
            //为属性picList赋值
            List<Pic> picList = scenicDao.queryPicListById(id);
            if (picList!=null&&picList.size()>0){
                System.out.println(picList);
                picList.remove(picList.size()-1);
                scenic.setPicList(picList);
            }
            //为属性ticketList赋值
            List<TScenicTicket> ticketList = scenicDao.queryTicketListById(id);
            if (ticketList!=null&&ticketList.size()>0){
                scenic.setTicketList(ticketList);
            }

            return scenic ;
        }
        return null;
    }
    @Override
    public int add(Scenic scenic) {
        return scenicDao.add(scenic);
    }

    @Override
    public int update(Scenic scenic) {
        try {
            scenicDao.update(scenic);
            return 1;
        } catch (Exception e) {
           e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return scenicDao.deleteById(id);
    }
}
